blog-banner

A great TypeScript database combo: TypeORM and CockroachDB

Last edited on September 14, 2021

0 minute read

    So you’re building a TypeScript application. You’ll need a database to power it, and an ORM to link your application to that database. We’d like to suggest a powerful combination that’s surprisingly easy to set up: TypeORM (a popular TypeScript ORM) and CockroachDB.

    This combination is so powerful, in fact, that we’ve created a narrative-driven tutorial about a powerful wizard that will help you get set up with CockroachDB and link your new distributed SQL database to your application.

    (You can also learn how to do this in our docs, but you’ll have more fun playing through the narrative tutorial!)

    Why CockroachDB and TypeORM?Copy Icon

    TypeORM is a TypeScript ORM (object-relational mapper) library that makes it easy to link your TypeScript application up to a relational database database.

    TypeORM supports MySQL, SQlite, Postgres, MS SQL Server, and a host of other traditional options. It also works seamlessly with CockroachDB, a cloud-native distributed database that combines the consistency advantages of a traditional RDBMS with the automated scaling, high availability, and survivability of a cloud-based distributed database.

    As you’ll find if you work through the tutorial, connecting your TypeScript application to CockroachDB is fast and straightforward.

    And since CockroachDB is available in a serverless cloud distribution for free, it raises the question: why not build your application with CockroachDB from the get-go? You can use the same familiar SQL you’d use with Postgres, but with a bunch of extra advantages, including:

    • Online schema changes – you can iterate fast and make changes without downtime.

    • Automated data replication – your data is distributed without you having to even think

      about nodes or shards.

    • Automated elastic scaling – skip the operational complexity and guesswork that often come with scaling by choosing a database that scales up and down as you need it, automatically.

    CockroachDB is also highly performant, so whether you’re working on a small side project or a web application that’ll be used by millions of people, it won’t slow you down.

    How to connect your TypeScript application to CockroachDBCopy Icon

    Our tutorial will walk you through this process in greater detail (as will our docs), but if you don’t need that level of detail to get started, here’s a quick overview of what it takes to link your TypeScript app to a cloud-based CockroachDB database:

    1. Sign up for a CockroachDB account and create a free cluster.

    2. Download the CockroachDB client and connect to your new cluster.

    3. Create your database using the same SQL commands you already know.

    4. Edit your application’s ormconfig.ts file with the relevant information to point it to your new CockroachDB database.

    5. Install the Node.js pg driver.

    6. Run your application and confirm that everything is working as expected.

    Again, the details are available in the narrative tutorial or our documentation if you need them. You can also get help at any time in our Slack community.

    tutorial